#ifndef __AP3216C_H__
#define __AP3216C_H__

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>


#define AP3216C_ADDR     0X1E /* AP3216C器件地址  */
/* AP3316C寄存器 */
#define AP3216C_SYSTEMCONG 0x00 /* 配置寄存器       */
#define AP3216C_INTSTATUS 0X01 /* 中断状态寄存器   */
#define AP3216C_INTCLEAR 0X02 /* 中断清除寄存器   */
#define AP3216C_IRDATALOW 0x0A /* IR数据低字节     */
#define AP3216C_IRDATAHIGH 0x0B /* IR数据高字节     */
#define AP3216C_ALSDATALOW 0x0C /* ALS数据低字节    */
#define AP3216C_ALSDATAHIGH 0X0D /* ALS数据高字节    */
#define AP3216C_PSDATALOW 0X0E /* PS数据低字节     */
#define AP3216C_PSDATAHIGH 0X0F /* PS数据高字节     */




#define AP3216C_POWER_DOWN   	 0x00
#define AP3216C_ALS_ENABLE     0x01
#define AP3216C_PS_IR_ENABLE   0x02
#define AP3216C_ENABLE_ALL     0x03
#define AP3216C_SW_RESET       0x04


//#define  PROXIMITY_THREASOLD    500
//#define  ALS_THREASOLD          50
//#define  IR_THREASOLD           20

// 定义阈值
#define PROXIMITY_THREASOLD 700                       // 接近传感器阈值

#define PROXIMITY_LEAVE_DESKTOP_THREASOLD 500          // 接近传感器阈值

#define ALS_THREASOLD 50                 // 环境光传感器阈值
#define BLACK_SURFACE_DETECTION_THRESHOLD 50    // 黑色桌面检测阈值
#define BLACK_SURFACE_TIME_THRESHOLD 5000  // 黑色桌面稳定时间阈值
#define FILTER_COEFFICIENT 0.1f           // 低通滤波器系数


#define LIGHT_INTERFERENCE_THRESHOLD  100
#define PROXIMITY_INTERFERENCE_THRESHOLD  100
// 状态定义
typedef enum{

 STATE_BLACK_SURFACE   = 0,      //黑色桌面
 STATE_NEAR_OBJECT     = 1,      //接近桌面
 STATE_IN_AIR          = 2,      //悬空状态
 STATE_IDLE            = 3,      //空闲状态

}State;

/*
	总共9种状态
STATE_BLACK_SURFACE ---> STATE_BLACK_SURFACE
STATE_BLACK_SURFACE ---> STATE_IN_AIR
STATE_BLACK_SURFACE ---> STATE_NEAR_OBJECT

STATE_NEAR_OBJECT ---> STATE_NEAR_OBJECT
STATE_NEAR_OBJECT ---> STATE_BLACK_SURFACE
STATE_NEAR_OBJECT ---> STATE_IN_AIR

STATE_IN_AIR      ---> STATE_IN_AIR
STATE_IN_AIR      ---> STATE_BLACK_SURFACE
STATE_IN_AIR      ---> STATE_NEAR_OBJECT

*/
typedef struct{
	  uint8_t id;
		bool is_init;
	  bool status;
	  bool last_status;
		bool is_black_desktop;
	
		uint16_t proximity_data;
		uint16_t last_proximity_data;
		uint16_t ambient_light;
		uint16_t last_ambient_light_data;
		uint16_t ir_data;

	
		uint16_t pins;
	  void (*Set_Out)(uint16_t pins,bool level);  // 设置引脚状态

 // 独立的状态和时间信息

 float filtered_proximity;
    uint32_t current_state;           // 当前状态
	uint32_t last_state;              // 上一次的状态
    uint32_t last_state_change_time;  // 上次状态改变的时间
    uint32_t last_black_surface_time; // 上次黑色桌面检测时间
	
	
	
}_ap3216c_t;



typedef struct{

	 uint8_t i2c_id;
	_ap3216c_t ap3216[4];

}apd3216_t;



void AP3216C_Init();

void AP3216C_Task();

uint32_t get_current_time_ms();
float filter_proximity_data(_ap3216c_t * ap3216,uint16_t current_value);



/* Raw I2C Commands */
static bool wireWriteByte(uint8_t val);
static bool wireWriteDataByte(uint8_t reg, uint8_t val);
static bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
static bool wireReadDataByte(uint8_t reg, uint8_t *val);
static int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);

#endif